package com.runfast.waimai.web.controller;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.paotui.dao.model.RunfastCuserExample;
import com.runfast.paotui.service.RunfastCuserService;
import com.runfast.waimai.dao.mapper.RunfastDataCenterMapper;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.RunfastBusinessExample;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentaccounts;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.utils.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by WQ on 2018/5/30.
 */
@RestController
@RequestMapping("/server")
public class IndexController {
    @Autowired
    private RunfastBusinessService businessService;
    @Autowired
    private RunfastDataCenterMapper dataCenterMapper;
    @Autowired
    private RunfastCuserService cuserService;

    /**
     * @param id agentId
     * @return
     */
    @GetMapping(value = {"", "index"})
    public Result index(Integer id) {
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts = SessionContext.getCurrentRunfastAgentaccounts();
        Map<String, Object> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        Date now = calendar.getTime();
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        calendar.add(Calendar.DATE, -1);
        Date yesterday = calendar.getTime();
        Date startOfYesterday = MyUtil.getStartDay(yesterday);
        Date endOfYesterday = MyUtil.getEndDay(yesterday);
        if (admin == null && agentAccounts == null) {
            return Result.fail(ResultCode.ACCOUNT_RELOGIN);
        }
        if (agentAccounts != null) {
            id = agentAccounts.getAgentid();
        }

        map.put("topList", dataCenterMapper.getTopList(id, startOfYesterday, endOfYesterday));

        Integer[] a = new Integer[]{1, 2};
        int temp = dayOfWeek - 2;
        if (temp < 0) {
            temp += 7;
        }
        Date thisMonday = MyUtil.addTimeSpan(now, 0, -temp);
        Date lastMonday = MyUtil.addTimeSpan(thisMonday, 0, -7);
        Date lastSunday = MyUtil.addTimeSpan(thisMonday, 0, -1);
        List<Map<String, Object>> thisWeek = new ArrayList<>();
        if (dayOfWeek != 2) {
            thisWeek = dataCenterMapper.orderDaily(id, thisMonday, endOfYesterday, Arrays.asList(a));
        }
        List<Map<String, Object>> lastWeek = dataCenterMapper.orderDaily(id, lastMonday, lastSunday, Arrays.asList(a));

        BigDecimal zero = new BigDecimal(0.0);
        BigDecimal[] thisWeekQuantity = new BigDecimal[temp];
        BigDecimal[] thisWeekAmount = new BigDecimal[temp];
        Arrays.fill(thisWeekQuantity, zero);
        Arrays.fill(thisWeekAmount, zero);
        Calendar calendar1 = Calendar.getInstance();
        for (Map<String, Object> map1 : thisWeek) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = sdf.parse(map1.get("date").toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            calendar1.setTime(date);
            int d = calendar1.get(Calendar.DAY_OF_WEEK);
            int t = d - 2;
            if (t < 0) {
                t += 7;
            }
            thisWeekQuantity[t] = new BigDecimal(map1.get("quantity1").toString());
            thisWeekAmount[t] = new BigDecimal(map1.get("amount1").toString());
        }
        map.put("thisWeekQuantity", thisWeekQuantity);
        map.put("thisWeekAmount", thisWeekAmount);

        BigDecimal[] lastWeekQuantity = new BigDecimal[7];
        BigDecimal[] lastWeekAmount = new BigDecimal[7];
        Arrays.fill(lastWeekQuantity, zero);
        Arrays.fill(lastWeekAmount, zero);
        for (Map<String, Object> map1 : lastWeek) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = sdf.parse(map1.get("date").toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            calendar1.setTime(date);
            int d = calendar1.get(Calendar.DAY_OF_WEEK);
            int t = d - 2;
            if (t < 0) {
                t += 7;
            }
            lastWeekQuantity[t] = new BigDecimal(map1.get("quantity1").toString());
            lastWeekAmount[t] = new BigDecimal(map1.get("amount1").toString());
        }
        map.put("lastWeekQuantity", lastWeekQuantity);
        map.put("lastWeekAmount", lastWeekAmount);

        Calendar calendar3 = Calendar.getInstance();
        int hour = calendar3.get(Calendar.HOUR_OF_DAY);
        int date = calendar3.get(Calendar.DATE);
        List<Map<String, Object>> hourlySum = new ArrayList<>();
        List<Map<String, Object>> pie1 = new ArrayList<>();
        List<Map<String, Object>> pie2 = new ArrayList<>();
        if (hour != 0) {
            calendar3.add(Calendar.HOUR_OF_DAY, -1);
            calendar3.set(Calendar.MINUTE, 59);
            calendar3.set(Calendar.SECOND, 59);
            Date end = calendar3.getTime();
            Date start = MyUtil.getStartDay(now);
            hourlySum = dataCenterMapper.hourlySumIndex(id, start, end);
            pie1 = dataCenterMapper.pie1(id, start, end);
            pie2 = dataCenterMapper.pie2(id, start, end);
        }

        BigDecimal[] pie11 = new BigDecimal[3];
        BigDecimal[] pie22 = new BigDecimal[2];
        Arrays.fill(pie11, zero);
        Arrays.fill(pie22, zero);
        for (Map<String, Object> map1 : pie1) {
            int payType = Integer.parseInt(map1.get("payType").toString());
            pie11[payType]=new BigDecimal(map1.get("price").toString());
        }
        for (Map<String, Object> map1 : pie2) {
            int flag = Integer.parseInt(map1.get("flag").toString());
            pie22[flag]=new BigDecimal(map1.get("price").toString());
        }
        map.put("pie1", pie11);
        map.put("pie2", pie22);

        BigDecimal[] hourlyQuantity = new BigDecimal[hour];
        BigDecimal[] hourlyAmount = new BigDecimal[hour];
        Arrays.fill(hourlyQuantity, zero);
        Arrays.fill(hourlyAmount, zero);
        for (Map<String, Object> map1 : hourlySum) {
            int hour1 = Integer.parseInt(map1.get("hour").toString());
            hourlyQuantity[hour1] = new BigDecimal(map1.get("quantity").toString());
            hourlyAmount[hour1] = new BigDecimal(map1.get("amount").toString());
        }
        map.put("hourlyQuantity", hourlyQuantity);
        map.put("hourlyAmount", hourlyAmount);

        List<Map<String, Object>> yesterdayHourlySum = dataCenterMapper.hourlySumIndex(id, startOfYesterday, endOfYesterday);
        BigDecimal[] yesterdayHourlyQuantity = new BigDecimal[24];
        BigDecimal[] yesterdayHourlyAmount = new BigDecimal[24];
        Arrays.fill(yesterdayHourlyQuantity, zero);
        Arrays.fill(yesterdayHourlyAmount, zero);
        for (Map<String, Object> map1 : yesterdayHourlySum) {
            int hour1 = Integer.parseInt(map1.get("hour").toString());
            yesterdayHourlyQuantity[hour1] = new BigDecimal(map1.get("quantity").toString());
            yesterdayHourlyAmount[hour1] = new BigDecimal(map1.get("amount").toString());
        }
        map.put("yesterdayHourlyQuantity", yesterdayHourlyQuantity);
        map.put("yesterdayHourlyAmount", yesterdayHourlyAmount);

        map.put("index", dataCenterMapper.index(id, startOfYesterday, endOfYesterday));

        Date firstDayOfMonth = MyUtil.getMonthStartDate(now);
        Date firstDayOfLastMonth = MyUtil.addTimeSpan(firstDayOfMonth, -1, 0);
        List<Map<String, Object>> thisMonth = new ArrayList<>();
        if (date > 1) {
            thisMonth = dataCenterMapper.orderDaily(id, firstDayOfMonth, endOfYesterday, Arrays.asList(a));
        }
        Date endDayOfLastMonth = MyUtil.getMonthEndDate(firstDayOfLastMonth);
        List<Map<String, Object>> lastMonth = dataCenterMapper.orderDaily(id, firstDayOfLastMonth, endDayOfLastMonth, Arrays.asList(a));

        BigDecimal[] thisMonthQuantity = new BigDecimal[date - 1];
        BigDecimal[] thisMonthAmount = new BigDecimal[date - 1];
        Arrays.fill(thisMonthQuantity, zero);
        Arrays.fill(thisMonthAmount, zero);
        for (Map<String, Object> map1 : thisMonth) {
            int d = Integer.parseInt(map1.get("dateOfMonth").toString());
            thisMonthQuantity[d - 1] = new BigDecimal(map1.get("quantity1").toString());
            thisMonthAmount[d - 1] = new BigDecimal(map1.get("amount1").toString());
        }
        map.put("thisMonthQuantity", thisMonthQuantity);
        map.put("thisMonthAmount", thisMonthAmount);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(endDayOfLastMonth);
        int lastMonthTotalDay = calendar2.get(Calendar.DAY_OF_MONTH);
        BigDecimal[] lastMonthQuantity = new BigDecimal[lastMonthTotalDay];
        BigDecimal[] lastMonthAmount = new BigDecimal[lastMonthTotalDay];
        Arrays.fill(lastMonthQuantity, zero);
        Arrays.fill(lastMonthAmount, zero);
        for (Map<String, Object> map1 : lastMonth) {
            int d = Integer.parseInt(map1.get("dateOfMonth").toString());
            lastMonthQuantity[d - 1] = new BigDecimal(map1.get("quantity1").toString());
            lastMonthAmount[d - 1] = new BigDecimal(map1.get("amount1").toString());
        }
        map.put("lastMonthQuantity", lastMonthQuantity);
        map.put("lastMonthAmount", lastMonthAmount);

        RunfastCuserExample cuserExample = new RunfastCuserExample();
        cuserExample.createCriteria().andCreateTimeBetween(startOfYesterday, endOfYesterday);
        map.put("yesterdayRegister", cuserService.countByExample(cuserExample));
        map.put("yesterday", dataCenterMapper.sumYesterday(id, startOfYesterday, endOfYesterday));
        map.put("yesterdaySubsidy", dataCenterMapper.sumSubsidy(id, startOfYesterday, endOfYesterday));

        RunfastBusinessExample example = new RunfastBusinessExample();
        RunfastBusinessExample.Criteria criteria = example.createCriteria();
        if (id != null)
            criteria.andAgentidEqualTo(id);
        map.put("businessCount", businessService.countByExample(example));
        criteria.andStatuEqualTo(0).andStatusEqualTo(0).andStatusxEqualTo(1);
        map.put("validBusinessCount", businessService.countByExample(example));
        //三日无单
        map.put("noOrdersInThreeBusinessCount", dataCenterMapper.countNoOrders(id, MyUtil.addTimeSpan(startOfYesterday, 0, -2), endOfYesterday));
        return Result.ok("", map);
    }
}
